﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BHXH_NT.Properties;
using System.Configuration;

namespace BHXH_NT
{
    public partial class MDIMain : Form
    {
       

        public MDIMain()
        {
            InitializeComponent();
        }               


        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tooptip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }
        public void Visibletooltip(bool flag)
        {
            tsbMahoa.Visible = flag;
            tsbCapnhat.Visible = flag;
            
        }
        private void MDIMain_Load(object sender, EventArgs e)
        {
         
            ModuleBHXH.huyen.NHATRANG = "NHATRANG";
            ModuleBHXH.huyen.CAMRANH = "CAMRANH";
            ModuleBHXH.huyen.CAMLAM = "CAMLAM";
            ModuleBHXH.huyen.NINHHOA = "NINHHOA";
            ModuleBHXH.huyen.VANNINH = "VANNINH";
            ModuleBHXH.huyen.DIENKHANH = "DIENKHANH";
            ModuleBHXH.huyen.KHANHVINH = "KHANHVINH";
            ModuleBHXH.huyen.KHANHSON = "KHANHSON";
            ModuleBHXH.huyen.TINH = "TINH";
            Visibletooltip(false);
            ModuleBHXH.sqlConnectionString = ModuleBHXH.Getstringconnection(ConfigurationManager.ConnectionStrings["dbConnectionString"].ConnectionString, ConfigurationManager.ConnectionStrings["dbVector"].ConnectionString);
            if (DateTime.Now.Month == 12)
            {
                ModuleBHXH.strMonths = string.Format("{0:0000}", DateTime.Now.Year+1) + string.Format("{0:00}", 1);
            }
            else
            {
                ModuleBHXH.strMonths = string.Format("{0:0000}", DateTime.Now.Year) + string.Format("{0:00}", DateTime.Now.Month);
          
            }
            if (BHXH_NT.Properties.Settings.Default.SERVER == 1) { timer1.Enabled = true; } else { timer1.Enabled = false; }
        }
        
        

        private void printPreviewToolStripButton_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form childForm = GetForm("frmKTHSCN");
            if (childForm == null)
            {
                childForm = new frmKTHSCN();
                childForm.MdiParent = this;
                childForm.Show();
                childForm.WindowState = FormWindowState.Maximized;
            }
            else
            {
                childForm.Activate();
            }
            Cursor.Current = Cursors.Default;
        }

       
        private Form GetForm(String FormName)
        {
            foreach (Form f in this.FindForm().MdiChildren)
            {
                if (f.Name == FormName)
                    return f;
            }
            return null;
        }

    
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            printPreviewToolStripButton_Click(null, null);
        }

        private void MDIMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F12 && e.Shift == true)
            {
                if (ModuleBHXH.admin == false)
                {
                    MessageBox.Show("Đăng nhập user admin");
                }
                else
                {
                    Cursor.Current = Cursors.WaitCursor;
                    Form childForm = GetForm("frmSetting");
                    if (childForm == null)
                    {
                        childForm = new frmSetting();
                        childForm.MdiParent = this;
                        childForm.Show();
                    }
                    else
                    {
                        childForm.Activate();
                    }
                    Cursor.Current = Cursors.Default;
                }
            }
            else if (e.KeyCode == Keys.L && e.Shift == true)
            {
                Cursor.Current = Cursors.WaitCursor;
                Form childForm = GetForm("FrmLogin");
                if (childForm == null)
                {
                    childForm = new FrmLogin();
                    childForm.MdiParent = this;
                    childForm.Show();
                }
                else
                {
                    childForm.Activate();
                }
                Cursor.Current = Cursors.Default;
            }

              
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form childForm = GetForm("frmAbout");
            if (childForm == null)
            {
                childForm = new frmAbout();
                //childForm.MdiParent = this;
                childForm.ShowDialog();
               
            }
            else
            {
                childForm.Activate();
            }
            Cursor.Current = Cursors.Default;
        }


        private void tsbAcount_Click(object sender, EventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;
            //Form childForm = GetForm("FrmAccount");
            //if (childForm == null)
            //{
            //    childForm = new FrmAccount();
            //    childForm.MdiParent = this;
            //    childForm.Show();
            //    childForm.WindowState = FormWindowState.Maximized;
            //}
            //else
            //{
            //    childForm.Activate();
            //}
            //Cursor.Current = Cursors.Default;
        }

        private void tsbLogin_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form childForm = GetForm("FrmLogin");
            if (childForm == null)
            {
                childForm = new FrmLogin();
                childForm.MdiParent = this;
                childForm.Show();
                
            }
            else
            {
                childForm.Activate();
            }
            Cursor.Current = Cursors.Default;
        }
        private bool TestConnection(string sqlConnection)
        {
            try
            {

                using (SqlConnection conn = new SqlConnection(sqlConnection))
                {
                    conn.Open();
                    conn.Close();

                }
            }
            catch 
            {
                return false;
            }

            return true;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (DateTime.Now.Hour  == global::BHXH_NT.Properties.Settings.Default.Timer.Hour &&
                DateTime.Now.Minute == global::BHXH_NT.Properties.Settings.Default.Timer.Minute && ModuleBHXH.admin == true)
            {
                if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_TINH_CONNECTIONTRING))
                {
                    if (UpdateDataFromHuyen(ModuleBHXH.huyen.TINH, BHXH_NT.Properties.Settings.Default.TINH_TIMER,
                        BHXH_NT.Properties.Settings.Default.MISBHXH_TINH_CONNECTIONTRING))
                    {
                        BHXH_NT.Properties.Settings.Default.TINH_TIMER = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    }
                }
                if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_NHATRANG_CONNECTIONTRING))
                {
                    if (UpdateDataFromHuyen(ModuleBHXH.huyen.NHATRANG, BHXH_NT.Properties.Settings.Default.NHATRANG_TIMER,
                        BHXH_NT.Properties.Settings.Default.MISBHXH_NHATRANG_CONNECTIONTRING))
                    {
                        BHXH_NT.Properties.Settings.Default.NHATRANG_TIMER = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    }
                }
                if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_CAMRANH_CONNECTIONTRING))
                {
                    if (UpdateDataFromHuyen(ModuleBHXH.huyen.CAMRANH, BHXH_NT.Properties.Settings.Default.CAMRANH_TIMER,
                        BHXH_NT.Properties.Settings.Default.MISBHXH_CAMRANH_CONNECTIONTRING))
                    {
                        BHXH_NT.Properties.Settings.Default.CAMRANH_TIMER = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    }
                }
                if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_CAMLAM_CONNECTIONTRING))
                {
                    if (UpdateDataFromHuyen(ModuleBHXH.huyen.CAMLAM, BHXH_NT.Properties.Settings.Default.CAMLAM_TIMER,
                        BHXH_NT.Properties.Settings.Default.MISBHXH_CAMLAM_CONNECTIONTRING))
                    {
                        BHXH_NT.Properties.Settings.Default.CAMLAM_TIMER = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    }
                }
                if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_NINHHOA_CONNECTIONTRING))
                {
                    if (UpdateDataFromHuyen(ModuleBHXH.huyen.NINHHOA, BHXH_NT.Properties.Settings.Default.NINHHOA_TIMER,
                        BHXH_NT.Properties.Settings.Default.MISBHXH_NINHHOA_CONNECTIONTRING))
                    {
                        BHXH_NT.Properties.Settings.Default.NINHHOA_TIMER = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    }
                }
                if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_VANNINH_CONNECTIONTRING))
                {
                    if (UpdateDataFromHuyen(ModuleBHXH.huyen.VANNINH, BHXH_NT.Properties.Settings.Default.VANNINH_TIMER,
                        BHXH_NT.Properties.Settings.Default.MISBHXH_VANNINH_CONNECTIONTRING))
                    {
                        BHXH_NT.Properties.Settings.Default.VANNINH_TIMER = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    }
                }
                if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_DIENKHANH_CONNECTIONTRING))
                {
                    if (UpdateDataFromHuyen(ModuleBHXH.huyen.DIENKHANH, BHXH_NT.Properties.Settings.Default.DIENKHANH_TIMER,
                        BHXH_NT.Properties.Settings.Default.MISBHXH_DIENKHANH_CONNECTIONTRING))
                    {
                        BHXH_NT.Properties.Settings.Default.DIENKHANH_TIMER = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    }
                }
                if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHVINH_CONNECTIONTRING))
                {
                    if (UpdateDataFromHuyen(ModuleBHXH.huyen.KHANHVINH, BHXH_NT.Properties.Settings.Default.KHANHVINH_TIMER,
                        BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHVINH_CONNECTIONTRING))
                    {
                        BHXH_NT.Properties.Settings.Default.KHANHVINH_TIMER = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    }
                }
                if (TestConnection(BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHSON_CONNECTIONTRING))
                {
                    if (UpdateDataFromHuyen(ModuleBHXH.huyen.KHANHSON, BHXH_NT.Properties.Settings.Default.KHANHSON_TIMER,
                        BHXH_NT.Properties.Settings.Default.MISBHXH_KHANHSON_CONNECTIONTRING))
                    {
                        BHXH_NT.Properties.Settings.Default.KHANHSON_TIMER = DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day;
                    }
                }
                
                global::BHXH_NT.Properties.Settings.Default.Save();
            }
        }
        
        private bool  UpdateDataFromHuyen(string huyen,string ngayps,string huyenconnectionstring)
        {
            dbConnection conn;
            conn = new dbConnection(huyenconnectionstring);
            DataTable dataTable;
            //insert ho so ca nhan
            string sql = "select *,'" + huyen + "' from hosocn where ngayps >= '" + ngayps + "' and left(sobhxh,1) <> 'Y'";
            SqlParameter[] sqlParameter = new SqlParameter[0];
            dataTable = conn.executeSelectQuery(sql, sqlParameter);
            if (dataTable != null)
            {
                using (SqlBulkCopy bulkCopy =
                            new SqlBulkCopy(ModuleBHXH.sqlConnectionString))
                {
                    bulkCopy.DestinationTableName = "HOSOCN_TEMP";
                    bulkCopy.WriteToServer(dataTable);
                }
                conn = new dbConnection(ModuleBHXH.sqlConnectionString);
                sql = " insert into HOSOCN "
                + " select * from dbo.HOSOCN_TEMP where huyen = '" + huyen + "'"
                + " and sobhxh not in (select sobhxh from HOSOCN WHERE HUYEN ='" + huyen + "')";
                conn.executeInsertQuery(sql, sqlParameter);
                
                sql = "delete HOSOCN_TEMP where huyen = '" + huyen + "'";
                conn.executeUpdateQuery(sql, sqlParameter);
            }
            else { return false; }
            
            sql = "SELECT *,'" + huyen + "' from HCHOSOCN where ngay >= '" + ngayps + "' and socmnd is not null and left(sobhxh,1) <> 'Y'";
            dataTable = conn.executeSelectQuery(sql, sqlParameter);
            if (dataTable != null)
            {
                using (SqlBulkCopy bulkCopy =
                           new SqlBulkCopy(ModuleBHXH.sqlConnectionString))
                {
                    bulkCopy.DestinationTableName = "HCHOSOCN";
                    bulkCopy.WriteToServer(dataTable);
                }
                // update ho so ca nhan
                sql = " update dbo.HOSOCN set SOCMND = b.socmnd "
                    + " from dbo.HOSOCN a inner join HCHOSOCN b "
                    + " on a.sobhxh = b.sobhxh where b.huyen = '" + huyen + "'";
                conn.executeInsertQuery(sql, sqlParameter);

                sql = "delete HCHOSOCN where huyen = '" + huyen + "'";
                conn.executeUpdateQuery(sql, sqlParameter);
            }
            else { return false; }
            return true;
        }

        private void Capnhat_Click(object sender, EventArgs e)
        {
            if( ModuleBHXH.admin == false )
            {
                MessageBox.Show("Đăng nhập user admin");
            }
            else
            {
                Cursor.Current = Cursors.WaitCursor;
                Form childForm = GetForm("frmCapnhatdulieu");
                if (childForm == null)
                {
                    childForm = new frmCapnhatdulieu();
                    childForm.MdiParent = this;
                    childForm.Show();
                    childForm.WindowState = FormWindowState.Maximized;
                }
                else
                {
                    childForm.Activate();
                }
                Cursor.Current = Cursors.Default;
                }
        }

        private void MDIMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                notifyIcon1.Visible = true;
                //notifyIcon1.ShowBalloonTip(300);
                this.ShowInTaskbar = false;
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            this.ShowInTaskbar = true;
            notifyIcon1.Visible = false;
        }

        private void tsbSosotam_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            Form childForm = GetForm("frmSosotam");
            if (childForm == null)
            {
                childForm = new frmSosotam();
                childForm.MdiParent = this;
                childForm.Show();
                childForm.WindowState = FormWindowState.Maximized;

            }
            else
            {
                childForm.Activate();
            }
            Cursor.Current = Cursors.Default;
        }

        private void tsbMahoa_Click(object sender, EventArgs e)
        {
            //Cursor.Current = Cursors.WaitCursor;
            //Form childForm = null;
            //if (childForm == null)
            //{
            //    childForm = new Encrypt_a_string.Form1();
            //    childForm.MdiParent = this;
            //    childForm.Show();
            //    childForm.WindowState = FormWindowState.Normal;

            //}
            //else
            //{
            //    childForm.Activate();
            //}
            //Cursor.Current = Cursors.Default;
        }



        
    }
}
